using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;

public static class Extensions
{
    /// <summary>
    ///  去掉字符串中的特殊字符
    /// </summary>
    /// <param name="chars"></param>
    /// <returns></returns>
    public static string ReplaceCharsByEmpty(this string chars)
    {
        chars = RemoveHtml(chars);
        chars = Regex.Replace(chars, "⊙", "");
        chars = Regex.Replace(chars, "⊙", "");
        chars = Regex.Replace(chars, "の", "");
        //用正则去掉所有标点符号
        chars = Regex.Replace(chars, "[\\s\\p{P}\n\r=<>$>+￥^]", "");
        chars = Regex.Replace(chars, "[ \\[ \\] \\^ \\-_*×――(^)（^）$%~!@#$…&%￥—+=<>《》【】!！??？:：•`·、。，；,.;\"‘’“”-]", "");
        //去掉转义字符
        chars = Regex.Replace(chars, @"'", "");
        chars = Regex.Replace(chars, @"\|", "");
        chars = Regex.Replace(chars, @"\\", "");
        chars = Regex.Replace(chars, @"\b", "");
        chars = Regex.Replace(chars, @"\n", "");
        chars = Regex.Replace(chars, @"\r\n", "");
        chars = Regex.Replace(chars, @"\t", "");
        chars = Regex.Replace(chars, @"\v", "");
        chars = Regex.Replace(chars, @"\f", "");
        chars = chars.Trim();
        return chars;
    }

    public static string RemoveHtml(this string @this)
    {
        StringBuilder stringBuilder = new StringBuilder(@this);
        StringBuilder stringBuilder2 = new StringBuilder();
        int num = 0;
        while (num < stringBuilder.Length)
        {
            char c = stringBuilder[num];
            num++;
            if (c == '<')
            {
                while (num < stringBuilder.Length)
                {
                    c = stringBuilder[num];
                    num++;
                    switch (c)
                    {
                        case '\'':
                            num = GetIndexAfterNextSingleQuote(stringBuilder, num, allowEscape: true);
                            continue;
                        case '"':
                            num = GetIndexAfterNextDoubleQuote(stringBuilder, num, allowEscape: true);
                            continue;
                        default:
                            continue;
                        case '>':
                            break;
                    }

                    break;
                }
            }
            else
            {
                stringBuilder2.Append(c);
            }
        }

        return stringBuilder2.ToString();
    }

    public static int GetIndexAfterNextDoubleQuote(this StringBuilder @this)
    {
        return GetIndexAfterNextDoubleQuote(@this, 0, allowEscape: false);
    }

    public static int GetIndexAfterNextDoubleQuote(this StringBuilder @this, bool allowEscape)
    {
        return GetIndexAfterNextDoubleQuote(@this, 0, allowEscape);
    }

    public static int GetIndexAfterNextDoubleQuote(this StringBuilder @this, int startIndex)
    {
        return GetIndexAfterNextDoubleQuote(@this, startIndex, allowEscape: false);
    }

    public static int GetIndexAfterNextDoubleQuote(this StringBuilder @this, int startIndex, bool allowEscape)
    {
        while (startIndex < @this.Length)
        {
            char c = @this[startIndex];
            startIndex++;
            char c2;
            if (allowEscape && c == '\\' && startIndex < @this.Length && ((c2 = @this[startIndex]) == '\\' || c2 == '"'))
            {
                startIndex++;
            }
            else if (c == '"')
            {
                return startIndex;
            }
        }

        return startIndex;
    }


    public static int GetIndexAfterNextSingleQuote(this StringBuilder @this)
    {
        return GetIndexAfterNextSingleQuote(@this, 0, allowEscape: false);
    }

    public static int GetIndexAfterNextSingleQuote(this StringBuilder @this, bool allowEscape)
    {
        return GetIndexAfterNextSingleQuote(@this, 0, allowEscape);
    }

    public static int GetIndexAfterNextSingleQuote(this StringBuilder @this, int startIndex)
    {
        return GetIndexAfterNextSingleQuote(@this, startIndex, allowEscape: false);
    }

    public static int GetIndexAfterNextSingleQuote(this StringBuilder @this, int startIndex, bool allowEscape)
    {
        while (startIndex < @this.Length)
        {
            char c = @this[startIndex];
            startIndex++;
            char c2;
            if (allowEscape && c == '\\' && startIndex < @this.Length && ((c2 = @this[startIndex]) == '\\' || c2 == '\''))
            {
                startIndex++;
            }
            else if (c == '\'')
            {
                return startIndex;
            }
        }

        return startIndex;
    }
}
